﻿using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.Dashboard;
using NewRetail.Application.Components.Dashboard.Model;
using NewRetail.Application.Components.WeChat.Model;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace NewRetail.Application.Utility.Hubs
{
    public interface ISignalrDealHelper : IApplicationService
    {
        /// <summary>
        /// 添加在线用户
        /// </summary>
        /// <param name="connctionId"></param>
        /// <param name="client"></param>
        void AddClient(string connctionId, SignalrOnOrOffLineData client);

        /// <summary>
        /// 判断数据中是否已经存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        SignalrOnOrOffLineData IsOnline(string name);
        /// <summary>
        /// 修改，由于刷新存在连接ID变化
        /// </summary>
        /// <param name="connctionId"></param>
        /// <param name="client"></param>
        void UpdateClient(string connctionId, SignalrOnOrOffLineData client);
        /// <summary>
        /// 移除下线用户
        /// </summary>
        /// <param name="connctionId"></param>
        void RemoveClient(string connctionId);

        IDictionary<string, SignalrOnOrOffLineData> GetClients();

        int ClientsCount();

        #region dashboard 获取数据的接口
        /// <summary>
        /// 获取基础信息概况
        /// </summary>
        /// <returns></returns>
        Task<HeaderResult<BaseInfoSummaryDto>> GetBasicInfoSummary();

        /// <summary>
        /// 获取今日销售
        /// </summary>
        /// <returns></returns>
        Task<HeaderResult<TodaySaleDto>> GetTodaySale();
        /// <summary>
        /// 获取近7日销售
        /// </summary>
        /// <returns></returns>
        Task<HeaderResult<LastSevenDaySaleDto>> GetLastSevenDaySale();
        /// <summary>
        /// 获取本年度销售
        /// </summary>
        /// <returns></returns>
        Task<HeaderResult<YearSaleDto>> GetYearSale();
        /// <summary>
        /// 今日各门店销售
        /// </summary>
        /// <returns></returns>
        Task<HeaderResult<TodayStoreSaleDto>> GetTodayStoreSale();
        /// <summary>
        /// 今日支付方式占比数据
        /// </summary>
        /// <returns></returns>
        Task<HeaderResult<IEnumerable<PaymentMethodSaleDto>>> GetPaymentWaySale();
        /// <summary>
        /// 今日购买渠道占比数据
        /// </summary>
        /// <returns></returns>
        Task<HeaderResult<IEnumerable<BuyChannelSaleDto>>> GetBuyChannelSale();

        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        HeaderResult<dynamic> GetDashboardTest();

        #endregion
    }

    public class SignalrDealHelper : ApplicationService, ISignalrDealHelper
    {
        private static IDictionary<string, SignalrOnOrOffLineData> onlines = new Dictionary<string, SignalrOnOrOffLineData>();
        public readonly IDashboardService _dashboardService;
        public ILogger<SignalrDealHelper> _logger;

        #region 构造方法
        public SignalrDealHelper(IDashboardService dashboardService, ILogger<SignalrDealHelper> logger)
        {
            _dashboardService = dashboardService;
            _logger = logger;
        }
        #endregion

        #region method

        #region 添加在线用户
        /// <summary>
        /// 添加在线用户
        /// </summary>
        /// <param name="connctionId"></param>
        /// <param name="client"></param>
        public void AddClient(string connctionId, SignalrOnOrOffLineData client)
        {
            onlines.Add(connctionId, client);
        }
        #endregion

        #region 判断数据中是否已经存在
        /// <summary>
        /// 判断数据中是否已经存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public SignalrOnOrOffLineData IsOnline(string name)
        {
            return onlines.Where(o => o.Value.IdentityName == name).FirstOrDefault().Value;
        }
        #endregion

        #region 修改，由于刷新存在连接ID变化
        /// <summary>
        /// 修改，由于刷新存在连接ID变化
        /// </summary>
        /// <param name="connctionId"></param>
        /// <param name="client"></param>
        public void UpdateClient(string connctionId, SignalrOnOrOffLineData client)
        {
            onlines[connctionId] = client;
        }
        #endregion

        #region 移除下线用户
        /// <summary>
        /// 移除下线用户
        /// </summary>
        /// <param name="connctionId"></param>
        public void RemoveClient(string connctionId)
        {
            if (onlines.Keys.Contains(connctionId))
                onlines.Remove(connctionId);
        }

        #endregion

        #region 获取在线用户
        /// <summary>
        /// 获取在线用户
        /// </summary>
        /// <returns></returns>
        public IDictionary<string, SignalrOnOrOffLineData> GetClients()
        {
            return onlines;
        }
        #endregion

        #region 获取在线数
        /// <summary>
        /// 获取在线数
        /// </summary>
        /// <returns></returns>
        public int ClientsCount()
        {
            return onlines.Count;
        }
        #endregion

        #region dashboard所有方法

        #region 获取基础信息概况
        /// <summary>
        /// 获取基础信息概况
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<BaseInfoSummaryDto>> GetBasicInfoSummary()
        {
            var result = await _dashboardService.BasicInfoSummary();
            var retMsg = new HeaderResult<BaseInfoSummaryDto>
            {
                Result = result,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #region 获取今日销售
        /// <summary>
        /// 获取今日销售
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<TodaySaleDto>> GetTodaySale()
        {
            var result = await _dashboardService.TodaySaleInfo();
            var retMsg = new HeaderResult<TodaySaleDto>
            {
                Result = result,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #region 获取近7日销售
        /// <summary>
        /// 获取近7日销售
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<LastSevenDaySaleDto>> GetLastSevenDaySale()
        {
            var result = await _dashboardService.LastSevenDaySaleInfo();
            var retMsg = new HeaderResult<LastSevenDaySaleDto>
            {
                Result = result,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #region 获取本年度销售
        /// <summary>
        /// 获取本年度销售
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<YearSaleDto>> GetYearSale()
        {
            var result = await _dashboardService.YearSaleInfo();
            var retMsg = new HeaderResult<YearSaleDto>
            {
                Result = result,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #region 今日各门店销售
        /// <summary>
        /// 今日各门店销售
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<TodayStoreSaleDto>> GetTodayStoreSale()
        {
            var result = await _dashboardService.TodayStoreSaleInfo();
            var retMsg = new HeaderResult<TodayStoreSaleDto>
            {
                Result = result,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #region 今日支付方式占比数据
        /// <summary>
        /// 今日支付方式占比数据
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<IEnumerable<PaymentMethodSaleDto>>> GetPaymentWaySale()
        {
            var result = await _dashboardService.PaymentWaySaleInfo();
            var retMsg = new HeaderResult<IEnumerable<PaymentMethodSaleDto>>
            {
                Result = result,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #region 今日购买渠道占比数据
        /// <summary>
        /// 今日购买渠道占比数据
        /// </summary>
        /// <returns></returns>
        public async Task<HeaderResult<IEnumerable<BuyChannelSaleDto>>> GetBuyChannelSale()
        {
            var result = await _dashboardService.BuyChannelSaleInfo();
            var retMsg = new HeaderResult<IEnumerable<BuyChannelSaleDto>>
            {
                Result = result,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #region 测试
        /// <summary>
        /// 获取xx
        /// </summary>
        /// <returns></returns>
        public HeaderResult<dynamic> GetDashboardTest()
        {
            dynamic data = new ExpandoObject();
            data.xData = new string[] { "11", "22", "33", "44", "55", "66" };
            data.seriesData = new int[] { 21, 121, 226, 30, 50, 110 };
            var retMsg = new HeaderResult<dynamic>
            {
                Result = data,
                IsSucceed = true
            };
            return retMsg;
        }
        #endregion

        #endregion

        #endregion
    }
}
